/**
 * 获取swiperList中current上一个的index
 * 正常 - 1
 * 或
 * 循环衔接到末尾
 */
var getLastSwiperChangeIndex = function(current, swiperList) {
	return current > 0 ? current - 1 : swiperList.length - 1
}

/**
 * 获取上一个要替换的list中的item
 */
var getLastSwiperItem = function(current, swiperList, list) {
	// list中我们需要的那个item的index
	let listNeedIndex = swiperList[current].index - 1
	// 如果要替换的下标超出了0，添加一个占位item
	let item = listNeedIndex == -1 ? {
		isFirstPlaceholder: true
	} : list[listNeedIndex]
	return item
}

/**
 * 改变下一项
 */
var changeNextItem = function(that, current) {
	let swiperChangeIndex = getNextSwiperChangeIndex(current, that.swiperList)
	let swiperChangeItem = "swiperList[" + swiperChangeIndex + "]"
	// console.log(swiperChangeItem);
	that.swiperList[swiperChangeIndex] = getNextSwiperItem(current, that.swiperList, that.questions)
}


/**
 * 获取swiperLit中current下一个的index
 * 正常 + 1
 * 或
 * 循环衔接到首位
 */
var getNextSwiperChangeIndex = function(current, swiperList) {
	return current < swiperList.length - 1 ? current + 1 : 0
}


/**
 * 获取下一个要替换的list中的item
 */
var getNextSwiperItem = function(current, swiperList, list) {
	// list中我们需要的那个item的index
	let listNeedIndex = swiperList[current].index + 1
	// 如果要替换的下标超出了list的下标，添加一个占位item
	let item = listNeedIndex == list.length ? {
		isLastPlaceholder: true
	} : list[listNeedIndex]
	return item
}

/**
 * 改变上一项
 */
var changeLastItem = function(that, current) {
	let swiperChangeIndex = getLastSwiperChangeIndex(current, that.swiperList)
	that.swiperList[swiperChangeIndex] = getLastSwiperItem(current, that.swiperList, that.questions)
}


/**
 * 获取初始化的swiperList
 */
var initSwiperList = function(that, lastDoQuestionIndex) {
	let list = that.questions
	let swiperListLength = that.swiperListLength
	let swiperList = []
	for (let i = 0; i < swiperListLength; i++) {
		swiperList.push({})
	}

	// current
	let current = lastDoQuestionIndex % swiperListLength
	swiperList[current] = list[lastDoQuestionIndex]

	// current的上一个
	let lastSwiperIndex = getLastSwiperChangeIndex(current, swiperList)
	swiperList[lastSwiperIndex] = getLastSwiperItem(current, swiperList, list)

	// current的下一个
	let nextSwiperIndex = getNextSwiperChangeIndex(current, swiperList)
	swiperList[nextSwiperIndex] = getNextSwiperItem(current, swiperList, list)
	//console.log(swiperList)
	that.question = that.questions[swiperList[current].index]
	that.swiperCurrent = current
	that.swiperIndex = current
	return swiperList;
}

/**
 * 页面改变
 */
var changePage = function(that, current) {
	let index = that.swiperList[current].index
	if(index == 0 || index ) {
		that.question = that.questions[that.swiperList[current].index]
	}
	let lastIndex = that.swiperIndex

	// 如果是滑到了左边界，再弹回去
	if (that.swiperList[current].isFirstPlaceholder) {
		// that.swiperCurrent = lastIndex
		that.swiperCurrent = current
		uni.showModal({
			title:'系统提示',
			content:'已经是第一题了',
			showCancel:false,
			success(){
				that.swiperCurrent = lastIndex
			}
		})
		return
	}
	// 如果滑到了右边界，弹回去，再弹个对话框
	if (that.swiperList[current].isLastPlaceholder) {
		// that.lastPage()
		that.swiperCurrent = current
		that.swiperConfig.autoplay = false
		// uni.showModal({
		// 	title:'系统提示',
		// 	content:'已经是最后一题了',
		// 	showCancel:false,
		// 	success(){
		// 		that.swiperCurrent = lastIndex
		// 	}
		// })
		// uni.showModal({
		// 	title: "提示",
		// 	content: "您已经答完所有题，是否退出？",
		// })
		return 'end'
	}

	// 正向滑动，到下一个的时候
	// 是正向衔接
	let isLoopPositive = current == 0 && lastIndex == that.swiperList.length - 1
	if (current - lastIndex == 1 || isLoopPositive) {
		changeNextItem(that, current)
	}

	// 反向滑动，到上一个的时候
	// 是反向衔接
	var isLoopNegative = current == that.swiperList.length - 1 && lastIndex == 0
	if (lastIndex - current == 1 || isLoopNegative) {
		changeLastItem(that, current)
	}

	// 记录滑过来的位置，此值对于下一次滑动的计算很重要
	that.swiperIndex = current
	that.swiperCurrent = current
	
	// console.log(that.question.question);
}

module.exports = {
	initSwiperList,
	changePage
}
